/*
 * @Author: 杨三炮
 * @Date: 2021-11-18 20:22:12
 * @LastEditors: 杨三炮
 * @LastEditTime: 2021-11-23 19:24:26
 * @FilePath: \node\node\dao\users_dao.js
 */
const keys = require('../config/keys').secretOrKey
const jwt = require('jsonwebtoken')
const md5 = require('md5')
module.exports = class users_dao extends require('../model/users_mod') {
    /**
     * @description: 登录
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async Login (req, res) {
        let password = md5(md5(req.body.password))
        let user = await this.searchUserMod(req.body.id)
        if (!user) {
            return res.status(404).json('用户不存在')
        }
        if (user.password != password) {
            return res.status(404).json('学号与密码不匹配')
        }
        if (user.type != Number(req.body.type)) {
            return res.status(404).json('请选择正确的身份')
        }
        const rule = {
            id: user.id,
            username: user.username,
            head: user.head,
            class_id: user.class_id,
            type: user.type
        }
        //jwt.sign('规则','加密名字','过期时间','箭头函数')
        jwt.sign(rule, keys, { expiresIn: 3600 }, (err, token) => {
            res.json({
                success: true,
                token: "Bearer " + token
            })
        })


    }


    /**
     * @description: 分页获取 可以接收query||params的参数
     * @param {*} req
     * @param {*} res
     * @return {*} data total
     */

    static async getUser (req, res) {
        let data = await this.getUserMod()
        res.json(data)
    }

    /**
     * @description: 删除用户同时清空该用户阅读记录
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async delUserdata (req, res) {

        const user = await this.searchUserMod(req.query.u_id)
        if (!user) {
            return res.status(404).json('用户不存在')
        }
        const result = await this.delUserdataMod(req.query.u_id)
        if (result.code != 200) {
            return res.status(404).json('删除失败')
        }
        res.json({ result })
    }

    /**
     * @description: 更改用户信息
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async upUserdata (req, res) {
        console.log(req.body);

        const user = await this.searchUserMod(req.body.id)
        if (!user) {
            return res.status(404).json('用户不存在')
        }
        let reqData = {
            u_id: req.body.id,
            username: req.body.username,
            sex: req.body.sex,
            address: req.body.address,
            class_id: req.body.class_id
        }
        try {
            let result = await this.upUserdataMod(reqData)
            res.json({ msg: '更新成功', code: 200, result, })
        } catch (error) {
            res.status(404).json(err)
        }


    }

    /**
     * @description: 插入用户 如果用户以存在则更新数据
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async setXlsxData (req, res) {
        console.log(req.body)
        let isUser = await this.searchUserMod(req.body.id)
        let data = {
            u_id: req.body.id,
            username: req.body.username,
            sex: req.body.sex,
            address: req.body.address,
            password: req.body.password,
            class_id: req.body.class_id
        }
        if (isUser) {
            try {
                await this.upUserdataMod(data)
                res.json({
                    msg: '数据更新成功',
                    code: 201,
                })
            } catch (error) {
                res.json({
                    msg: error,
                    code: 0,
                })
            }
        } else {
            try {
                await this.setXlsxDataMod(data)
                res.json({
                    msg: '添加用户成功',
                    code: 200
                })
            } catch (error) {
                res.json({
                    msg: '添加失败',
                    code: 0,
                })
            }
        }

    }
    /**
     * @description: 修改用户密码
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async upPwd (req, res) {
        let oldPwd = md5(md5(req.body.old_pwd))
        let newPwd = md5(md5(req.body.new_pwd2))
        let u_id = req.user.id
        const user = await this.searchUserMod(u_id)
        if (!user) {
            return res.json({
                code: 0,
                msg: '用户不存在'
            })

        }
        try {
            const result = await this.upPwdMod(u_id, oldPwd, newPwd, user)
            console.log(result)
            if (result.code == 201) {
                res.status(404).json(result.msg)

            } else {
                res.json(result)
            }
        } catch (error) {
            res.status(404).json('服务器异常')
        }
    }


    static async getUserInfo (req, res) {
        const user = await this.searchUserMod(req.user.id)
        res.json(user)
    }

}